
local LuaMongo = _G.lua_mongo
local Util = require("common.Util")
local DB = require("common.DB")

local queryTowerByLv = { lv = nil }


function addTower(msg)
    local data = query(msg.lv)
    if data ~= nil then
        return
    end    

    local auTb ={}
	auTb.lv = msg.lv
    auTb.combatVideo  = msg.combatVideo
    auTb.headNum = msg.headNum
    auTb.headList =  msg.headList
    auTb.roleNum = msg.roleNum   
	LuaMongo.insert(DB.db_tower, auTb)
end


function query(lv, fileds)
    queryTowerByLv.lv = lv
    local data = {}
    LuaMongo.find(DB.db_tower, queryTowerByLv, fileds)
	if not LuaMongo.next(data) then
		return nil
	end
    return data
end


function addVideoToTower(data, uuid, time, timeUp, zhanliUp, combatTime, zhandouli, combat)
    data.combatVideo[1] = data.combatVideo[1] or {}
    data.combatVideo[1].time =  time
    data.combatVideo[1].uuid = uuid
    data.combatVideo[1].zhandouli = zhandouli
    data.combatVideo[1].combatTime = combatTime
    data.combatVideo[1].combatVideo = combat
   

    if timeUp > 0 then
        data.combatVideo[2] = data.combatVideo[2] or {}
        data.combatVideo[2].time =  time
        data.combatVideo[2].uuid = uuid
        data.combatVideo[2].zhandouli = zhandouli
        data.combatVideo[2].combatTime = combatTime
        data.combatVideo[2].combatVideo = combat
    end 

    if zhanliUp > 0 then
        data.combatVideo[3] = data.combatVideo[3] or {}
        data.combatVideo[3].time =  time
        data.combatVideo[3].uuid = uuid
        data.combatVideo[3].zhandouli = zhandouli
        data.combatVideo[3].combatTime = combatTime
        data.combatVideo[3].combatVideo = combat
    end

end

function updateDB(data)
	queryTowerByLv.lv = data.lv
	LuaMongo.update(DB.db_tower, queryTowerByLv, data)
end